package cn.tedu.cwgl.service.impl;

import cn.tedu.cwgl.entity.Bill;
import cn.tedu.cwgl.entity.Payway;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.mapper.BillMapper;
import cn.tedu.cwgl.service.BillService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

@Service
public class BillServiceImpl implements BillService {

    @Resource
    private BillMapper billMapper;

    @Override
    public Result<Bill> findByWhereNoPage(Bill bill) {
        List<Bill> bills = billMapper.findByWhereNoPage(bill);
        if (bills.size() >= 0) {
            Result<Bill> result = ResultUtil.success(bills);
            result.setMsg("数据获取成功");
            return result;
        } else {
            return ResultUtil.unSuccess("没有找到符合条件的属性！！");
        }
    }

    @Override
    public Result<Bill> findByWhere(PageModel model) {
        List<Bill> bills = billMapper.findByWhere(model);
        if (bills.size() >= 0) {
            Result<Bill> result = ResultUtil.success(bills);
            result.setTotal(billMapper.getTotalByWhere(model));
            if (result.getTotal() == 0) {
                result.setMsg("查询不到任何相关数据");
            } else {
                result.setMsg("数据获取成功");
            }
            return result;
        } else {
            return ResultUtil.unSuccess("数据获取失败");
        }
    }

    @Override
    public List<Payway> getAllPayways() {
        return billMapper.getAllPayways();
    }

    private UserInfo getSessionUser(HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        userInfo.setPassword(null);
        return userInfo;
    }

    @Override
    public int addBill(Bill bill, HttpSession httpSession) {
//获取时间
        Date ss = new Date();

        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format0.format(ss.getTime());
        bill.setTime(time);
//获取姓名
        UserInfo userInfo1 = getSessionUser(httpSession);

        bill.setUserid(userInfo1.getId());


        int result = billMapper.addBill(bill);
        return result;
    }

    @Override
    public int deleteBill(Bill bill) {
        return billMapper.deleteBill(bill);
    }

    @Override
    public int updateBill(Bill bill) {
        return billMapper.updateBill(bill);
    }


}
